package sfms.view;

import java.awt.event.*;
import java.beans.*;
import java.sql.Time;
import javax.swing.JOptionPane;
import javax.swing.table.*;
import sfms.model.*;
import sfms.util.*;

public class Staff extends javax.swing.JFrame {

    private java.util.Date minDate = new java.util.Date();
    private sfms.controller.Staff controller;

    public Staff(sfms.controller.Staff controller) {
        initComponents();
        setLocationRelativeTo(null);
        this.controller = controller;
        minDate.setDate(minDate.getDate()+7);
        dateReservations.setMinSelectableDate(minDate);
        //Removes the ID column from the tableView but not the tableModel
        tableFacility.removeColumn(tableFacility.getColumnModel().getColumn(0));
        
        addHooks();
    }
    
    private void addHooks(){
        controller.setModelBooking((DefaultTableModel)this.tableBookings.getModel());
        controller.setModelUsers((DefaultTableModel)this.tableUsers.getModel());
        controller.setModelFacilities((DefaultTableModel)this.tableFacility.getModel());

        dateReservations.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent pce) {
                try{
                    setActivityOptions(1);
                    buttonReserve.setEnabled(isActivitySelected());
                }
                catch(Exception e){}
            }
        });
        
        dateBookings.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent pce) {
                try{
                    controller.setBookings(dateBookings.getDate());
                }
                catch(Exception e){}
            }
        });
        
        buttonDeleteUser.addActionListener(controller.getDeleteUserListener());
        controller.addUsers();        
    }
    
    
    
    /*
    public void setCancelBookingEnabled(boolean b){
        buttonCancelBooking.setEnabled(b);
    }
    */
    public boolean isActivitySelected(){
        return (dateReservations.getDate() != null) &&
        (selectFacility.getSelectedIndex() > 0) &&
        (selectSlot.getSelectedIndex() > 0) &&
        (selectVenue.getSelectedIndex() > 0);
    }
    
    public void setActivityOptions(int comboBoxNum){
        switch(comboBoxNum){
            case 0://We aren't adding anything.
                dateReservations.setDate(null);
            case 1:
                String facText = (String) selectFacility.getItemAt(0);
                selectFacility.removeAllItems();
                selectFacility.addItem(facText);
            case 2:
                String slotText = (String) selectSlot.getItemAt(0);
                selectSlot.removeAllItems();
                selectSlot.addItem(slotText);
            case 3:
                String venueText = (String) selectVenue.getItemAt(0);
                selectVenue.removeAllItems();
                selectVenue.addItem(venueText);
        }
        
        switch(comboBoxNum){
            case 1:
                if (dateReservations.getDate() != null){
                    System.out.println(dateReservations.getDate());
                    CustomDate d = new CustomDate(dateReservations.getDate());
                    for (Object o: controller.getFacilities(d)){
                        selectFacility.addItem(o);
                    }
                }
                break;
            case 2:
                if (selectFacility.getSelectedIndex() > 0){
                    Facility f = (Facility) selectFacility.getSelectedItem();
                    for (Object o: controller.getSlots(f, new CustomDate(dateReservations.getDate()))){
                        selectSlot.addItem(o);
                    }
                }
                break;
            case 3:
                if (selectSlot.getSelectedIndex() > 0){
                    Facility f = (Facility) selectFacility.getSelectedItem();
                    for (Object o: controller.getVenues(f)){
                        selectVenue.addItem(o);
                    }
                }
                break;
        }
    }

    
    
    
    void createUser(String username, String fname, String lname, String email, String number, String role, boolean active, java.sql.Date expiry, String password)
    {
        controller.createUser(username, fname, lname, role, active, number, email, expiry, password);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cMainPanel = new javax.swing.JPanel();
        tabStaff = new javax.swing.JTabbedPane();
        bookingsTab = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableBookings = new javax.swing.JTable();
        dateBookings = new com.toedter.calendar.JDateChooser();
        jLabel1 = new javax.swing.JLabel();
        reservationsTab = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        dateReservations = new com.toedter.calendar.JDateChooser();
        jLabel5 = new javax.swing.JLabel();
        selectFacility = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        selectSlot = new javax.swing.JComboBox();
        selectVenue = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        buttonReserve = new javax.swing.JButton();
        facilitiesTab = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableFacility = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();
        buttonCreateFacility = new javax.swing.JButton();
        buttonEditFacility = new javax.swing.JButton();
        usersTab = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tableUsers = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        buttonCreateUser = new javax.swing.JButton();
        buttonEditUser = new javax.swing.JButton();
        buttonDeleteUser = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        itemLogout = new javax.swing.JMenuItem();
        itemExit = new javax.swing.JMenuItem();
        menuSettings = new javax.swing.JMenu();
        itemChangePassword = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sports Facility Management System");
        setMinimumSize(new java.awt.Dimension(640, 480));
        setName("MainWindow"); // NOI18N

        cMainPanel.setLayout(new java.awt.BorderLayout());

        jLabel3.setFont(new java.awt.Font("Lucida Grande", 0, 36)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("View Bookings");

        tableBookings.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Facility Type", "Time", "Venue"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableBookings.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableBookings.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tableBookings);
        tableBookings.getColumnModel().getColumn(0).setResizable(false);
        tableBookings.getColumnModel().getColumn(1).setResizable(false);
        tableBookings.getColumnModel().getColumn(2).setResizable(false);
        tableBookings.getColumnModel().getColumn(2).setHeaderValue("Venue");

        dateBookings.setDateFormatString("yyyy-MM-dd");
        dateBookings.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dateBookingsMouseClicked(evt);
            }
        });

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel1.setText("Select Date:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dateBookings, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(dateBookings, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout bookingsTabLayout = new javax.swing.GroupLayout(bookingsTab);
        bookingsTab.setLayout(bookingsTabLayout);
        bookingsTabLayout.setHorizontalGroup(
            bookingsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, bookingsTabLayout.createSequentialGroup()
                .addContainerGap(107, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(106, Short.MAX_VALUE))
        );
        bookingsTabLayout.setVerticalGroup(
            bookingsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bookingsTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabStaff.addTab("View Bookings", bookingsTab);

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 0, 36)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Make Reservations");

        dateReservations.setDateFormatString("yyyy-MM-dd");
        dateReservations.setFocusable(false);
        dateReservations.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dateReservationsMouseClicked(evt);
            }
        });

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel5.setText("Select Date:");

        selectFacility.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "- Select Facility -" }));
        selectFacility.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectFacilityActionPerformed(evt);
            }
        });

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Select Facility Type:");

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Select Available Slots:");

        selectSlot.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "- Select Time Slot -" }));
        selectSlot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectSlotActionPerformed(evt);
            }
        });

        selectVenue.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "- Select a Venue -" }));
        selectVenue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectVenueActionPerformed(evt);
            }
        });

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Select Venue:");

        buttonReserve.setText("Make Reservation");
        buttonReserve.setEnabled(false);
        buttonReserve.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonReserveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(selectSlot, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(selectFacility, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(selectVenue, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(buttonReserve, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(9, 9, 9)
                                .addComponent(dateReservations, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(dateReservations, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selectFacility, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selectSlot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selectVenue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonReserve)
                .addContainerGap(369, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout reservationsTabLayout = new javax.swing.GroupLayout(reservationsTab);
        reservationsTab.setLayout(reservationsTabLayout);
        reservationsTabLayout.setHorizontalGroup(
            reservationsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, reservationsTabLayout.createSequentialGroup()
                .addContainerGap(103, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(104, Short.MAX_VALUE))
        );
        reservationsTabLayout.setVerticalGroup(
            reservationsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(reservationsTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabStaff.addTab("Make Reservations", reservationsTab);

        tableFacility.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "Facility Type", "Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableFacility.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableFacility.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tableFacility);
        tableFacility.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableFacility.getColumnModel().getColumn(1).setResizable(false);
        tableFacility.getColumnModel().getColumn(2).setResizable(false);

        jLabel8.setFont(new java.awt.Font("Lucida Grande", 0, 36)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("Manage Facilities");

        buttonCreateFacility.setText("Create New Facility");
        buttonCreateFacility.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCreateFacilityActionPerformed(evt);
            }
        });

        buttonEditFacility.setText("Edit Facility");
        buttonEditFacility.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditFacilityActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(buttonCreateFacility, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonEditFacility, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCreateFacility)
                    .addComponent(buttonEditFacility))
                .addGap(17, 17, 17))
        );

        javax.swing.GroupLayout facilitiesTabLayout = new javax.swing.GroupLayout(facilitiesTab);
        facilitiesTab.setLayout(facilitiesTabLayout);
        facilitiesTabLayout.setHorizontalGroup(
            facilitiesTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(facilitiesTabLayout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(81, 81, 81))
        );
        facilitiesTabLayout.setVerticalGroup(
            facilitiesTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(facilitiesTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(56, Short.MAX_VALUE))
        );

        tabStaff.addTab("Manage Facilities", facilitiesTab);

        tableUsers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Username", "First Name", "Last Name", "Expiry", "Email", "Phone", "Active"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableUsers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableUsers.getTableHeader().setReorderingAllowed(false);
        jScrollPane3.setViewportView(tableUsers);
        tableUsers.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        jLabel9.setFont(new java.awt.Font("Lucida Grande", 0, 36)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("Manage Users");

        buttonCreateUser.setText("Create New User");
        buttonCreateUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCreateUserActionPerformed(evt);
            }
        });

        buttonEditUser.setText("Edit User");
        buttonEditUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditUserActionPerformed(evt);
            }
        });

        buttonDeleteUser.setText("Delete User");
        buttonDeleteUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDeleteUserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(buttonDeleteUser, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(buttonCreateUser, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonEditUser, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonCreateUser)
                    .addComponent(buttonEditUser)
                    .addComponent(buttonDeleteUser))
                .addContainerGap())
        );

        javax.swing.GroupLayout usersTabLayout = new javax.swing.GroupLayout(usersTab);
        usersTab.setLayout(usersTabLayout);
        usersTabLayout.setHorizontalGroup(
            usersTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(usersTabLayout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(105, Short.MAX_VALUE))
        );
        usersTabLayout.setVerticalGroup(
            usersTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(usersTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabStaff.addTab("Manage Users", usersTab);

        cMainPanel.add(tabStaff, java.awt.BorderLayout.CENTER);

        getContentPane().add(cMainPanel, java.awt.BorderLayout.CENTER);

        menuFile.setText("File");

        itemLogout.setText("Log Out");
        itemLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemLogoutActionPerformed(evt);
            }
        });
        menuFile.add(itemLogout);

        itemExit.setText("Exit");
        itemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemExitActionPerformed(evt);
            }
        });
        menuFile.add(itemExit);

        menuBar.add(menuFile);

        menuSettings.setText("Settings");

        itemChangePassword.setText("Change Password");
        itemChangePassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemChangePasswordActionPerformed(evt);
            }
        });
        menuSettings.add(itemChangePassword);

        menuBar.add(menuSettings);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void itemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemExitActionPerformed
       dispose();
    }//GEN-LAST:event_itemExitActionPerformed

    private void selectVenueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectVenueActionPerformed
        buttonReserve.setEnabled(isActivitySelected());
    }//GEN-LAST:event_selectVenueActionPerformed

    private void buttonReserveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonReserveActionPerformed
        controller.bookActivity(
            ((Venue) selectVenue.getSelectedItem()).getId(),
            (Time) selectSlot.getSelectedItem(),
            new CustomDate(dateReservations.getDate()).getSQLDate());
    }//GEN-LAST:event_buttonReserveActionPerformed

    private void buttonCreateFacilityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCreateFacilityActionPerformed
        controller.createFacility();
    }//GEN-LAST:event_buttonCreateFacilityActionPerformed
     
    private void buttonCreateUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCreateUserActionPerformed
        //controller.createUser();
        CreateUser createUser = new CreateUser(this, true, controller);
        createUser.setVisible(true);
    }//GEN-LAST:event_buttonCreateUserActionPerformed

    private void buttonDeleteUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteUserActionPerformed
        if (tableUsers.getSelectedRow() >= 0) {
            controller.editUser(true, (String) tableUsers.getValueAt(tableUsers.getSelectedRow(), 0));
        }
    }//GEN-LAST:event_buttonDeleteUserActionPerformed

    private void selectFacilityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectFacilityActionPerformed
        setActivityOptions(2);
        buttonReserve.setEnabled(isActivitySelected());
    }//GEN-LAST:event_selectFacilityActionPerformed

    private void selectSlotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectSlotActionPerformed
        setActivityOptions(3);
        buttonReserve.setEnabled(isActivitySelected());
    }//GEN-LAST:event_selectSlotActionPerformed

    private void buttonEditUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditUserActionPerformed
        try {
            if (tableUsers.getSelectedRow() != -1) {
                controller.editUser(false, (String) tableUsers.getValueAt(tableUsers.getSelectedRow(), 0));
            }
        } catch (Exception e) {}
    }//GEN-LAST:event_buttonEditUserActionPerformed

    private void itemLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemLogoutActionPerformed
        controller.logout();
    }//GEN-LAST:event_itemLogoutActionPerformed

    private void buttonEditFacilityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditFacilityActionPerformed
        // TODO add your handling code here:
        if (tableFacility.getSelectedRow() != -1){
            controller.editFacility(tableFacility.getSelectedRow());
        }
    }//GEN-LAST:event_buttonEditFacilityActionPerformed

    private void dateBookingsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dateBookingsMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_dateBookingsMouseClicked

    private void itemChangePasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemChangePasswordActionPerformed
        try {
            String password = ChangePassword.getPassword();
            if (ChangePassword.getLastError() != null) {
                JOptionPane.showMessageDialog(this, ChangePassword.getLastError(), "Error", JOptionPane.ERROR_MESSAGE);
            } else {
                if (controller.changePassword(password)) {
                    JOptionPane.showMessageDialog(this, "Your password has been changed.", "Success", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, new String[]{"Your password was not changed. Please", "ensure your password was entered correctly."}, "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        } catch (Exception e) {}
    }//GEN-LAST:event_itemChangePasswordActionPerformed

    private void dateReservationsMouseClicked(MouseEvent e) {
        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bookingsTab;
    private javax.swing.JButton buttonCreateFacility;
    private javax.swing.JButton buttonCreateUser;
    private javax.swing.JButton buttonDeleteUser;
    private javax.swing.JButton buttonEditFacility;
    private javax.swing.JButton buttonEditUser;
    private javax.swing.JButton buttonReserve;
    private javax.swing.JPanel cMainPanel;
    private com.toedter.calendar.JDateChooser dateBookings;
    private com.toedter.calendar.JDateChooser dateReservations;
    private javax.swing.JPanel facilitiesTab;
    private javax.swing.JMenuItem itemChangePassword;
    private javax.swing.JMenuItem itemExit;
    private javax.swing.JMenuItem itemLogout;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu menuFile;
    private javax.swing.JMenu menuSettings;
    private javax.swing.JPanel reservationsTab;
    private javax.swing.JComboBox selectFacility;
    private javax.swing.JComboBox selectSlot;
    private javax.swing.JComboBox selectVenue;
    private javax.swing.JTabbedPane tabStaff;
    private javax.swing.JTable tableBookings;
    private javax.swing.JTable tableFacility;
    private javax.swing.JTable tableUsers;
    private javax.swing.JPanel usersTab;
    // End of variables declaration//GEN-END:variables

    public int getUserRow() {
        return tableUsers.getSelectedRow();
    }
}
